//Program Modified by Jeffrey Parkinson
//CS 342 Programming Assignment 3
//Purpose - To draw a bezier surface and allow the user
//          to rotate, change control points, and view as
//          a wire mesh or solid surface 

/*
 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED 
 * Permission to use, copy, modify, and distribute this software for 
 * any purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright notice
 * and this permission notice appear in supporting documentation, and that 
 * the name of Silicon Graphics, Inc. not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. 
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * US Government Users Restricted Rights 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor
 * clauses in the FAR or the DOD or NASA FAR Supplement.
 * Unpublished-- rights reserved under the copyright laws of the
 * United States.  Contractor/manufacturer is Silicon Graphics,
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
 *
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
 */

/*  bezsurf.c
 *  This program renders a wireframe Bezier surface,
 *  using two-dimensional evaluators.
 */
#include <stdlib.h>
#include <GL/glut.h>

GLfloat ctrlpoints[4][4][3] = {
   {{-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0}, 
    {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}}, 
   {{-1.5, -0.5, 1.0}, {-0.5, -0.5, 3.0}, 
    {0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}}, 
   {{-1.5, 0.5, 4.0}, {-0.5, 0.5, 0.0}, 
    {0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}}, 
   {{-1.5, 1.5, -2.0}, {-0.5, 1.5, -2.0}, 
    {0.5, 1.5, 0.0}, {1.5, 1.5, -2.0}}
};

//Global variables that will be used in multiple areas of the program
GLfloat angle = 45.0;
int Mesh = 0;
int selected = 0;

GLfloat ambient[] = {0.9, 0.2, 0.2, 1.0};
GLfloat position[] = {0.0, 0.0, 2.0, 1.0};
GLfloat mat_diffuse[] = {0.6, 0.6, 0.6, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};

//This will display the surface as a solid surface or
//as a wire frame.
void display(void)
{
   int i, j;
   glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
           0, 1, 12, 4, &ctrlpoints[0][0][0]);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glColor3f(.9, .7, 0.0);
   glPushMatrix (); 
    glRotatef(angle,1.0, 1.0, 1.0); 
    if(Mesh == 1)
    {
      //This is for the mesh and allows for object lighting
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
      glLightfv(GL_LIGHT0, GL_POSITION, position);

      glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
      glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
      glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
      glEvalMesh2(GL_FILL, 0, 20, 0, 20);
      glDisable(GL_LIGHTING);
      glDisable(GL_LIGHT0);
    } else 
    {
      //This is for the wire frame and just uses normal color 
      for (j = 0; j <= 8; j++) {
       glBegin(GL_LINE_STRIP);
       for (i = 0; i <= 30; i++)
         glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
       glEnd();
       glBegin(GL_LINE_STRIP);
       for (i = 0; i <= 30; i++)
         glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
       glEnd();
      }
    }
    //This is for the control points to be displayed and the
    //current selected one will be red instead of white     
    glPointSize(10.0);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_POINTS);
      for (i = 0; i < 4; i++)
      {
	for (j = 0; j < 4; j++)
	{
           if (selected == 4 * i  + j)
	   {    
		glColor3f(1.0, 0.0, 0.0);
	   }
	   else
	   {
		glColor3f(1.0, 1.0, 1.0);
	   }
	   glVertex3f(ctrlpoints[i][j][0], ctrlpoints[i][j][1], ctrlpoints[i][j][2]);
	}
      }
    glEnd();
   glPopMatrix ();
 
   glFlush();
}

//This will initialize variables and functions needed throughout the program
void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel(GL_SMOOTH);
   glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
           0, 1, 12, 4, &ctrlpoints[0][0][0]);
   glEnable(GL_MAP2_VERTEX_3);
   glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);
}

//This will resize the surface based upon window size
void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= h)
      glOrtho(-4.0, 4.0, -4.0*(GLfloat)h/(GLfloat)w, 
              4.0*(GLfloat)h/(GLfloat)w, -4.0, 4.0);
   else
      glOrtho(-4.0*(GLfloat)w/(GLfloat)h, 
              4.0*(GLfloat)w/(GLfloat)h, -4.0, 4.0, -4.0, 4.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
   int i, j;
   switch (key) {
      case 27:
         exit(0);
         break;
      //These button presses will rotate the object 45 degrees
      case '0':
	 angle = 0.0;
	 break; 
      case '1':
         angle = 45.0;
	 break;
      case '2':
	 angle = 90.0;
	 break;
      case '3':
         angle = 135.0;
	 break;
      case '4':
         angle = 180.0;
	 break;
      case '5':
	 angle = 225.0;
	 break;
      case '6':
         angle = 270.0;
	 break;
      case '7':
         angle = 315.0;
	 break;
      case '8':
	 angle = 360.0;
	 break;
      //This will allow the surface to be viewed as a mesh
      case 'm':
      case 'M':
         Mesh = 1;
	 break;
      //This will allow the surface to be viewed as a wire frame
      case 'w':
      case 'W':
         Mesh = 0;
	 break;
      //This will increment the x part of the selected control point
      case '.':
	 if(selected >= 4 && selected < 8)
	 {
		ctrlpoints[1][selected - 4][0] += 1;
	 } else if (selected >= 8 && selected < 12)
	 {
                ctrlpoints[2][selected - 8][0] += 1;
	 } else if (selected >= 12)
	 {
                ctrlpoints[3][selected - 12][0] += 1;
	 } else
	 {
                ctrlpoints[0][selected][0] += 1;
	 } 
	 break;      
      //This will decrement the x part of the selected control point
      case '/':
	 if(selected >= 4 && selected < 8)
	 {
		ctrlpoints[1][selected - 4][0] -= 1;
	 } else if (selected >= 8 && selected < 12)
	 {
                ctrlpoints[2][selected - 8][0] -= 1;
	 } else if (selected >= 12)
	 {
                ctrlpoints[3][selected - 12][0] -= 1;
	 } else
	 {
                ctrlpoints[0][selected][0] -= 1;
	 } 
	 break;
      //This will increment the y part of the selected control point 
     case ';':
	 if(selected >= 4 && selected < 8)
	 {
		ctrlpoints[1][selected - 4][1] += 1;
	 } else if (selected >= 8 && selected < 12)
	 {
                ctrlpoints[2][selected - 8][1] += 1;
	 } else if (selected >= 12)
	 {
                ctrlpoints[3][selected - 12][1] += 1;
	 } else
	 {
                ctrlpoints[0][selected][1] += 1;
	 } 
	 break;     
      //This will decrement the y part of the selected control point 
      case '\'':
	 if(selected >= 4 && selected < 8)
	 {
		ctrlpoints[1][selected - 4][1] -= 1;
	 } else if (selected >= 8 && selected < 12)
	 {
                ctrlpoints[2][selected - 8][1] -= 1;
	 } else if (selected >= 12)
	 {
                ctrlpoints[3][selected - 12][1] -= 1;
	 } else
	 {
                ctrlpoints[0][selected][1] -= 1;
	 } 
	 break;
      //This will increment the z part of the selected control point
      case '[':
	 if(selected >= 4 && selected < 8)
	 {
		ctrlpoints[1][selected - 4][2] += 1;
	 } else if (selected >= 8 && selected < 12)
	 {
                ctrlpoints[2][selected - 8][2] += 1;
	 } else if (selected >= 12)
	 {
                ctrlpoints[3][selected - 12][2] += 1;
	 } else
	 {
                ctrlpoints[0][selected][2] += 1;
	 } 
	 break;      
      //This will decrement the z part of the selected control point
      case ']':
	 if(selected >= 4 && selected < 8)
	 {
		ctrlpoints[1][selected - 4][2] -= 1;
	 } else if (selected >= 8 && selected < 12)
	 {
                ctrlpoints[2][selected - 8][2] -= 1;
	 } else if (selected >= 12)
	 {
                ctrlpoints[3][selected - 12][2] -= 1;
	 } else
	 {
                ctrlpoints[0][selected][2] -= 1;
	 } 
	 break;
   }

   glutPostRedisplay();
}

void handleMouse(int button, int state, int x, int y)
{
	int i;

	//This will cycle forward through the control points for
        //each left mouse click
	if ((button == GLUT_LEFT_BUTTON) && state == GLUT_DOWN)
	{
		if ( selected <= 14)
		{
			selected += 1;
		}
		else
		{
			selected = 0;
		} 
		glutPostRedisplay();
	}

	//This will cycle backwards through the control points for
        //each right mouse click
	if ((button == GLUT_RIGHT_BUTTON) && state == GLUT_DOWN)
	{
		if ( selected >= 1)
		{
			selected -= 1;
		}
		else
		{
			selected = 15;
		} 
		glutPostRedisplay();
	}
}

//The main section of the program
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMouseFunc(handleMouse); 
   glutMainLoop();
   return 0;
}


